package com.yfp.operation.controller;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.web.controller.BaseController;
import com.yfp.common.core.web.page.TableDataInfo;
import com.yfp.operation.domain.MarAssociation;
import com.yfp.operation.domain.PositionDO;
import com.yfp.operation.domain.WxGroupChat;
import com.yfp.operation.domain.WxGroupMember;
import com.yfp.operation.service.MarAssociationService;
import com.yfp.operation.service.PositionService;
import com.yfp.operation.service.WxGroupChatService;
import com.yfp.operation.service.WxGroupMemberService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description: 社群
 *
 * @CreateDate: 2024/7/5 14:31
 * @UpdateUser:
 * @UpdateDate: 2024/7/5 14:31
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/marAssociation")
@Tag(name = "社群")
public class MarAssociationController extends BaseController {
    @Autowired
    private MarAssociationService marAssociationService;
    @Autowired
    private PositionService positionService;
    @Autowired
    private WxGroupMemberService wxGroupMemberService;
    @Autowired
    private WxGroupChatService wxGroupChatService;

    @Operation(summary = "社群列表")
    @GetMapping("/list")
    public R<TableDataInfo<List<MarAssociation>>> list(MarAssociation marAssociation) {
        startPage();
        TableDataInfo dataTable = getDataTable(marAssociationService.findList(marAssociation));
        return R.ok(dataTable);
    }

    @Operation(summary = "社群详情")
    @GetMapping("/{id}")
    public R<MarAssociation> findById(@PathVariable("id") Long id) {
        return R.ok(marAssociationService.getById(id));
    }

    @Operation(summary = "修改社群")
    @PutMapping("/edit")
    public R<Void> edit(@RequestBody MarAssociation marAssociation){
        marAssociationService.edit(marAssociation);
        return R.ok();
    }

    @Operation(summary = "新增社群")
    @PostMapping("/add")
    public R<Void> add(@RequestBody MarAssociation marAssociation) throws WxErrorException {
        List<MarAssociation> marAssociations = marAssociationService.findByPositionId(marAssociation.getPositionId());
        if (!marAssociations.isEmpty() ){
            return R.fail("该点位已添加，不能重复添加");
        }
        MarAssociation marAssociation1 = marAssociationService.findByGroupChatId(marAssociation.getGroupChatId());
        if (null != marAssociation1){
            return R.fail("该群已添加点位，不能重复添加");
        }
        WxGroupChat wxGroupChat = wxGroupChatService.getById(marAssociation.getGroupChatId());
        if (null == wxGroupChat || wxGroupChat.getStatus() == 0){
            return R.fail("该群已解散，请重新添加");
        }
        marAssociationService.add(marAssociation);
        return R.ok();
    }

    @Operation(summary = "获取所有点位信息")
    @PostMapping("/findPositions")
    public R<List<PositionDO>> findPositions(){
        List<PositionDO> positionDOS = positionService.findAll();
        return R.ok(positionDOS);
    }

    @Operation(summary = "同步微信群列表")
    @PostMapping("/syncGroupChat")
    public R<Void> syncGroupChat() throws WxErrorException {
        marAssociationService.syncGroupChat();
        return R.ok();
    }

    @Operation(summary = "获取群员列表")
    @GetMapping("/findGroupMemberList")
    public R<TableDataInfo<List<WxGroupMember>>> findGroupMemberList(WxGroupMember wxGroupMember){
        startPage();
        TableDataInfo dataTable = getDataTable( wxGroupMemberService.findList(wxGroupMember));
        return R.ok(dataTable);
    }
    @Operation(summary = "配置群聊方式")
    @GetMapping("/groupConfig")
    public R<String> groupConfig(String groupChatId) throws WxErrorException {
        String string = marAssociationService.configGroupChat("test", groupChatId);
        return R.ok(string);
    }

}